﻿using MvvmFoundation.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace ServiceOperater.ViewModels
{
    /*
        TreeView中的每一个TreeViewItem的数据源
     *   代表一个文件夹/文件
     */
    public sealed class FileTreeNodeViewModel : ObservableObject
    {

        public FileTreeNodeViewModel(string text, string fullPath, FileTreeNodeViewModel parent)
        {
            this.Text = text;
            this.FullPath = fullPath;
            this.Parent = parent;
            this.Children = new ObservableCollection<FileTreeNodeViewModel>();
            this.ServiceCollection = new ObservableCollection<ServiceTreeNodeViewModel>();
        }

        public FileTreeNodeViewModel(string text, string fullPath, bool isDirectory, FileTreeNodeViewModel parent)
            : this(text, fullPath, parent)
        {
            this.IsDirectory = isDirectory;
        }

        private void instantiateCommand()
        {
            QueryCommand = new RelayCommand<Object>((obj) =>
            {
                // System.Windows.Controls.TreeViewItem

            }, (obj) =>
            {
                return true;
            });
        }

        #region Props
        // 
        private string text = string.Empty;
        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                if (text == value)
                {
                    return;
                }

                text = value;

                // TODO 保存起来！

                RaisePropertyChanged("Text");
            }
        }

        // 是否目录
        public bool IsDirectory { get; private set; }

        private bool isExpanded = true;
        public bool IsExpanded
        {
            get
            {
                return isExpanded;
            }
            set
            {
                if (isExpanded == value)
                {
                    return;
                }

                isExpanded = value;

                RaisePropertyChanged("IsExpanded");
            }
        }

        private bool isSelected = false;
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                if (isSelected == value)
                {
                    return;
                }

                isSelected = value;

                RaisePropertyChanged("IsSelected");
            }
        }

        public FileTreeNodeViewModel Parent { get; private set; }

        public ObservableCollection<FileTreeNodeViewModel> Children { get; private set; }

        // 完整路径
        public String FullPath { get; set; }

        // 文件中的Service集合
        private ObservableCollection<ServiceTreeNodeViewModel> serviceCollection;
        public ObservableCollection<ServiceTreeNodeViewModel> ServiceCollection
        {
            get
            {
                return serviceCollection;
            }
            set
            {
                if (serviceCollection == value)
                {
                    return;
                }

                serviceCollection = value;

                RaisePropertyChanged("ServiceCollection");
            }
        }
        #endregion

        #region Commands

        public RelayCommand<Object> QueryCommand { get; private set; }

        #endregion

        #region 枚举数据源
        //public IEnumerable<ValueDescription> QXDMList
        //{ get { return EnumHelper.GetAllValuesAndDescriptions<QXDM>(); } }

        //public IEnumerable<ValueDescription> DJLXList
        //{ get { return EnumHelper.GetAllValuesAndDescriptions<DJLX>(); } }
        #endregion

        public void AddChild(FileTreeNodeViewModel child)
        {
            // TODO 先保存
            this.Children.Add(child);
        }

        //public void RemoveChild(TreeNodeViewModel child)
        //{
        //    // TODO 先保存
        //    this.children.Add(child);
        //}
    }
}
